package me.numark.view;

import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.tencent.open.utils.Util;

import java.util.ArrayList;

import me.numark.R;
import me.numark.utils.Utils;

/**
 * Created by kHRYSTAL on 2015/8/17.
 */
public class TagPopup extends PopupWindow{
    private Context mContext;

    protected final int LIST_PADDING = 10;

    private Rect mRect = new Rect();

    private final int[] mLocation = new int[2];

    private int mScreenWidth,mScreenHeight;

    private boolean mIsDirty;

    private int popupGravity = Gravity.NO_GRAVITY;

    private OnItemOnClickListener mItemOnClickListener;

    private GridView mGridView;

    private ArrayList<TagItem> mActionItems = new ArrayList<TagItem>();

    public TagPopup(Context context){
        this(context, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    }

    public TagPopup(Context context, int width, int height){
        this.mContext = context;

        setFocusable(true);
        setTouchable(true);
        setOutsideTouchable(true);

        mScreenWidth = Utils.getScreenWidth(mContext);
        mScreenHeight = Utils.getScreenHeight(mContext);

        setWidth(width);
        setHeight(height);

        setBackgroundDrawable(new BitmapDrawable());
        setContentView(LayoutInflater.from(mContext).inflate(R.layout.tag_popup, null));

        initUI();
    }


    private void initUI(){
        mGridView = (GridView) getContentView().findViewById(R.id.tag_grid);

        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int index, long arg3) {
                dismiss();
                if (mItemOnClickListener != null)
                    mItemOnClickListener.onItemClick(mActionItems.get(index), index);
            }
        });
    }


    public void show(View view){

        view.getLocationOnScreen(mLocation);

        mRect.set(mLocation[0], mLocation[1], mLocation[0] + view.getWidth(),mLocation[1] + view.getHeight());

        if(mIsDirty){
            populateActions();
        }

        showAtLocation(view, popupGravity, LIST_PADDING + (getWidth()/2), mRect.bottom);
    }


    private void populateActions(){
        mIsDirty = false;
        mGridView.setAdapter(new BaseAdapter() {
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                TextView textView = null;

                if (convertView == null) {
                    textView = new TextView(mContext);
                    textView.setTextColor(mContext.getResources().getColor(android.R.color.white));
                    textView.setTextSize(14);
                    textView.setGravity(Gravity.CENTER);
                    textView.setPadding(0, 10, 0, 10);
                    textView.setSingleLine(true);
                } else {
                    textView = (TextView) convertView;
                }

                TagItem item = mActionItems.get(position);

                textView.setText(item.getTag());
                return textView;
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public Object getItem(int position) {
                return mActionItems.get(position);
            }

            @Override
            public int getCount() {
                return mActionItems.size();
            }
        }) ;
    }


    public void addTags(TagItem action){
        if(action != null){
            mActionItems.add(action);
            mIsDirty = true;
        }
    }


    public void cleanTags(){
        if(!mActionItems.isEmpty()){
            mActionItems.clear();
            mIsDirty = true;
        }
    }


    public TagItem getTags(int position){
        if(position < 0 || position > mActionItems.size())
            return null;
        return mActionItems.get(position);
    }


    public void setItemOnClickListener(OnItemOnClickListener onItemOnClickListener){
        this.mItemOnClickListener = onItemOnClickListener;
    }


    public static interface OnItemOnClickListener{
        public void onItemClick(TagItem item , int position);
    }
}
